package com.yvon.maple.service.msg.core.websocket.processor;

import cn.hutool.json.JSONUtil;
import com.yvon.maple.service.msg.core.websocket.message.LoginMessage;
import com.yvon.maple.service.msg.core.websocket.message.ResponseMessage;
import com.yvon.maple.service.msg.core.websocket.session.Session;
import com.yvon.maple.service.msg.core.websocket.util.SessionUtil;
import io.netty.channel.Channel;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;

import java.time.LocalDateTime;
import java.util.Objects;

/**
 * @author : Yvon
 * @since : 2021-12-02
 */
public class LoginProcessor extends Processor<LoginMessage>{

    @Override
    public void exec(Channel channel, String msg) {
        LoginMessage message = parse(msg);
        if (Objects.nonNull(message)) {
            String username = message.username;
            SessionUtil.bindSession( new Session(username), channel);
            ResponseMessage responseMessage = new ResponseMessage(username, LocalDateTime.now(), "登陆成功");

            channel.writeAndFlush(new TextWebSocketFrame(JSONUtil.toJsonStr(responseMessage)));
        }
    }
}
